// pages/i/store-order-refund.js
const orderService = require('../../services/order');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: 0,
        o: {},
        items: [],
        refund: {},
        sender_phone: '',
        refund_reason: '', // 退款原因
        submitting: false // 提交状态
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            id: options.order_id
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.loadOrderDetail();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    async loadOrderDetail() {
        const result = await orderService.one(this.data.id);
        if (result.success) {
            this.setData({
                o: result.data.order,
                items: result.data.items,
                refund: result.data.refund,
                sender_phone: result.data.order.sender_phone
            });
        }
    },

    // 输入退款原因
    onRefundReasonInput(e) {
        this.setData({
            refund_reason: e.detail
        });
    },

    // 输入联系电话
    onPhoneInput(e) {
        this.setData({
            sender_phone: e.detail
        });
    },

    // 提交退款
    async submitRefund() {
        const { id, refund_reason, sender_phone } = this.data;
        
        // 验证必填字段
        if (refund_reason == '') {
            wx.showToast({
                title: '请输入退款原因',
                icon: 'none'
            });
            return;
        }

        if (sender_phone == '') {
            wx.showToast({
                title: '请输入联系电话',
                icon: 'none'
            });
            return;
        }

        this.setData({ submitting: true });

        try {
            const refundData = {
                order_id: id,
                refund_reason: refund_reason,
                phone: sender_phone
            };

            const result = await orderService.refund(refundData);
            
            if (result.success) {
                wx.showToast({
                    title: '申请提交成功',
                    icon: 'success'
                });
                
                // 延迟返回上一页
                setTimeout(() => {
                    wx.navigateBack();
                }, 1500);
            } else {
                wx.showToast({
                    title: result.message || '退款申请提交失败',
                    icon: 'none'
                });
            }
        } catch (error) {
            console.error('提交退款失败:', error);
            wx.showToast({
                title: '网络错误，请重试',
                icon: 'none'
            });
        } finally {
            this.setData({ submitting: false });
        }
    }
})